﻿Imports Microsoft.Samples.Windows.Forms.Navigation
Imports System.Text
Imports System.Diagnostics

Public Class Startup
    <STAThread()> _
    Public Shared Sub Main()
        '        AddHandler Application.ThreadException, AddressOf Application_ThreadException
        Application.EnableVisualStyles()
        ' Create a new Explorer window and show the home page in it.
        Dim homePage As Page = New Home()
        Dim e As New Explorer(homePage)
        e.ApplicationName = "Trade Confirms Archive"

        App.Instance.UserID = 1 'TODO: Change to use login
        Application.Run(e)
    End Sub

    Private Shared Sub Application_ThreadException(ByVal sender As Object, ByVal e As System.Threading.ThreadExceptionEventArgs)
        LogException(e.Exception)
        MessageBox.Show("An unexpected error occured. If this happens again please close and restart the application." + Environment.NewLine + Environment.NewLine + e.Exception.Message, "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub

    Private Shared Sub LogException(ByVal currentException As Exception)
        Try
            Dim message As New StringBuilder()

            While (currentException IsNot Nothing)
                message.AppendFormat("" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "type={0}" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "message={1}" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "stack=" & Chr(13) & "" & Chr(10) & "{2}" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "", currentException.[GetType]().FullName, currentException.Message, currentException.StackTrace)
                currentException = currentException.InnerException
            End While

            Dim Log As New EventLog()
            Log.Source = Application.ProductName
            Log.WriteEntry(message.ToString(), EventLogEntryType.[Error])

        Catch ex As Exception
            'Swallow exception nothing much we can do here.
        End Try
    End Sub
End Class
